package cn.worldyao.homework.service.impl;

import cn.worldyao.homework.Tools;
import cn.worldyao.homework.entity.Student;
import cn.worldyao.homework.mapper.StudentMapper;
import cn.worldyao.homework.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yaozi
 * @since 2020-05-11
 */
@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Override
    public boolean checkSLoginNameExist(String loginName) {
        log.info("\n-----checkLoginNameExist-[{}]----\n", loginName);
        QueryWrapper<Student> queryWrapper;
        queryWrapper = new QueryWrapper();
        queryWrapper.eq("LOGIN_NAME", loginName);
        int i = this.count(queryWrapper);
        return i > 0;
    }

    @Override
    public Student sLogin(Student student) {
        Student stu = this.getSOneByLoginName(student.getLoginName());
        if (stu != null) {
            String ePass = Tools.MD5Pwd(student.getPassword(), stu.getSalt());
            if (ePass.equals(stu.getPassword())) {
                return stu;
            } else {
                return null;
            }
        }
        return null;
    }

    @Override
    public Student getSOneByLoginName(String loginName) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("LOGIN_NAME", loginName);
        return this.getOne(wrapper);
    }
}
